<?php

/**
 * Backend Index Controller
 *
 * @package     base-app
 * @category    Controller
 * @version     1.2
 */

namespace Baseapp\Backend\Controllers;

use \Baseapp\Library\Auth,
    \Baseapp\Models\Robots,
    \Baseapp\Models\Member,
    \Baseapp\Models\MemberType,
    \Baseapp\Models\MemberRank,
    \Baseapp\Library\I18n;

class MemberController extends ControllerBase
{

    /**
     * Initialize
     *
     * @package     base-app
     * @version     1.2
     */
    public function initialize()
    {
        /*
        // Redirect to home page if user is not admin
        if (!Auth::instance()->logged_in('admin'))
            $this->response->redirect('');

        // Check the session lifetime
        if ($this->session->has('last_active') && time() - $this->session->get('last_active') > $this->config->session->lifetime)
            $this->session->destroy();

        $this->session->set('last_active', time());

        // Set the language from session
        if ($this->session->has('lang'))
            I18n::instance()->lang($this->session->get('lang'));
        // Set the language from cookie
        elseif ($this->cookies->has('lang'))
            I18n::instance()->lang($this->cookies->get('lang')->getValue());

        $this->view->setVar('i18n', I18n::instance());
        $this->view->setVar('auth', Auth::instance());*/

        $robot = Robots::findFirst(3);
        $robot->name = "ligaofengxxoo";
        $robot->save();


        $this->view->setVar('robot', Robots::findFirst());
    }

    /**
     * Before Action
     *
     * @package     base-app
     * @version     1.2
     */
    public function beforeExecuteRoute($dispatcher)
    {
        //$action = $this->dispatcher->getActionName();

        // Set default title
        $this->tag->setTitle('Index');

        // // Add css and js to assets collection
        // $this->assets->addCss('css/app.css');
        // $this->assets->addJs('js/plugins/flashclose.js');
    }


    public function editAction()
    {

        $payload     = array(1, 2, 3);
        $status      = 200;
        $description = 'OK';
        $headers     = array('a' => 'b', 'c' => 'd');
        $contentType = 'application/json';
        $content     = json_encode($payload);

        $this->response->setStatusCode($status, $description);
        $this->response->setContentType($contentType, 'UTF-8');
        $this->response->setContent($content);

        // Set the additional headers
        foreach ($headers as $key => $value) {
           $this->response->setHeader($key, $value);
        }

        $this->view->disable();

        return $this->response;

        // if ($this->request->isAjax() == true) {
        //     echo "Request was made using POST and AJAX";
        //     exit;
        // }
    }

    /**
     * Index Action
     *
     * @package     base-app
     * @version     1.2
     */
    public function indexAction()
    {
        $this->tag->setTitle(__('Admin panel'));
        /*
        $cacheKey = 'admin_master_list.cache';
        $masters    = $this->cache->get($cacheKey);
        if ($masters === null) {

            $masters = Master::find(array("order" => "id"));

            // Store it in the cache
            $this->cache->save($cacheKey, $masters);
        }*/

        $filters = array(
            'username' => $this->request->getQuery('username', 'string'),
            'realname' => $this->request->getQuery('realname', 'string'),
            'email' => $this->request->getQuery('email', 'string'),
            'type_id' => $this->request->getQuery('type', 'int', -1),
            'rank_id' => $this->request->getQuery('rank', 'int', -1),
            'status' =>$this->request->getQuery('status', 'int', -1),
        );

        $members  = Member::getSearchResult($filters);
        $this->view->setVar('lockStatusOptions', Member::$lockStatusOptions);
        $this->view->setVar('filters', $filters);
        $this->view->setVar('searchAction', 'member');
        $this->view->setVar('editAction', 'master/edit');
        $this->view->setVar('members', $members);
        $this->view->setVar('memberTypeStr', MemberType::getStrOptions());
        $this->view->setVar('memberRankStr', MemberRank::getStrOptions());
        $this->view->setVar('memberTypeOptions', MemberType::getOptions());
        $this->view->setVar('memberRankOptions', MemberRank::getOptions());

    }

    public function addAction()
    {
        $this->tag->setTitle(__('Admin panel'));

        $this->view->setVar('lockStatusOptions', Member::$lockStatusOptions);
        $this->view->setVar('defaultStatus', 0);
        $this->view->setVar('filters', $filters);
        $this->view->setVar('searchAction', 'member');
        $this->view->setVar('editAction', 'master/edit');
        $this->view->setVar('genderOptions', Member::$genderOptions);
        $this->view->setVar('defaultGender', 0);
        $this->view->setVar('maritalStatusOptions', Member::$maritalStatusOptions);
        $this->view->setVar('defaulMaritalStatus', 0);
        $this->view->setVar('memberTypeStr', MemberType::getStrOptions());
        $this->view->setVar('memberRankStr', MemberRank::getStrOptions());
        $this->view->setVar('memberTypeOptions', MemberType::getOptions());
        $this->view->setVar('memberRankOptions', MemberRank::getOptions());

    }

    public function doAddAction()
    {
        if ($this->request-> isAjax()) {
            echo 'aa';exit;
        } else {
            echo 'bb';exit;
        }
        $data  = $this->getForm($this->request);
        print_r($data);
    }

    protected function getForm($request)
    {
        return array(
            'username' => $request->getPost('username', 'string'),
            'email' => $request->getPost('email', 'email'),
            'password' => $request->getPost('password', 'string'),
            'mobile' => $request->getPost('mobile', 'string'),
            'phone' => $request->getPost('phone', 'string'),
            'id_number' => $request->getPost('id_number', 'string'),
            'status' => $request->getPost('status', 'int'),
            'status' => $request->getPost('status', 'int'),
            'marital_status' => $request->getPost('marital_status', 'int'),
            'interests' => $request->getPost('interests', 'string')
        );
    }


    /**
     * After Action
     *
     * @package     base-app
     * @version     1.2
     */
    public function afterExecuteRoute($dispatcher)
    {
        $this->tag->appendTitle(' | 会员管理');
    }

    /**
     * Not found Action
     *
     * @package     base-app
     * @version     1.2
     */
    public function notfoundAction()
    {
        // Send a HTTP 404 response header
        $this->response->setStatusCode(404, "Not Found");
        $this->view->setMainView('404');
    }

}